package com.meter.data.protocol.result;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.meter.basic.domain.BasicMeter;
import com.meter.basic.service.impl.BasicMeterServiceImpl;
import com.meter.common.utils.DateUtils;
import com.meter.common.utils.HexUtils;
import com.meter.common.utils.StringUtils;
import com.meter.common.utils.spring.SpringUtils;
import com.meter.data.domain.MeterData;
import com.meter.data.service.impl.MeterDataServiceImpl;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class Meter03 {

    /**
     * 保存恩勒曼热表数据
     * @param msgs
     */
    public static void saveMeter(String[] msgs, MeterData meterData){
        String meterNo = HexUtils.strChangeStrByLength(msgs, 7, 4, false);
        try {
            //热量
            String heatStr = Integer.parseInt(msgs[22] + msgs[21]) + "." + Integer.parseInt(msgs[24] + msgs[23]);
            double heat = Double.parseDouble(heatStr);
            //流量
            String flowStr = msgs[30] + msgs[29] + msgs[28] + msgs[27];
            //热量单位
            String lldw = msgs[26];
            double flow = 0.0;
            if (lldw.equals("13")) {
                flow = Double.parseDouble(flowStr) * 0.001;
            } else if (lldw.equals("14")) {
                flow = Double.parseDouble(flowStr) * 0.01;
            } else if (lldw.equals("15")) {
                flow = Double.parseDouble(flowStr) * 0.1;
            }
            //当前功率
            String powerStr = msgs[36] + msgs[35] + msgs[34] + msgs[33];
            double power = 0.0;
            if (msgs[35].equals("DD") && msgs[36].equals("DD")) {
                power = 0;
            } else {
                power = Double.parseDouble(powerStr) * 0.001;
            }
            String instantStr = msgs[41] + msgs[40] + msgs[39];
            double instant = 0.0;
            if (msgs[41].equals("DD")) {
                instant = 0.0;
            } else {
                instant = Double.parseDouble(instantStr) * 0.001;
            }
            //进水温度
            double inlet = Double.parseDouble(msgs[45] + msgs[44]) * 0.1;
            //回水温度
            double outlet = Double.parseDouble(msgs[49] + msgs[48]) * 0.1;
            //工作时间
            String workTime = Integer.parseInt(msgs[57] + msgs[56]) * 60 + "";
            //运行时间
            String yxsj = msgs[63] + msgs[62] + msgs[61] + msgs[60];
            //将16进制数据转为二进制数组
            String[] strs = HexUtils.hexStringBinStr(yxsj, 32);
            //计算分钟
            String mm = HexUtils.get10Num(strs[26] + strs[27] + strs[28] + strs[29] + strs[30] + strs[31]).toString();
            if (mm.length() < 2) {
                mm = "0" + mm;
            }
            //计算小时
            String HH = HexUtils.get10Num(strs[19] + strs[20] + strs[21] + strs[22] + strs[23]).toString();
            if (HH.length() < 2) {
                HH = "0" + HH;
            }
            //计算日期
            String MM = HexUtils.get10Num(strs[4] + strs[5] + strs[6] + strs[7]).toString();
            if (MM.length() < 2) {
                MM = "0" + MM;
            }
            //计算月份
            String DD = HexUtils.get10Num(strs[11] + strs[12] + strs[13] + strs[14] + strs[15]).toString();
            if (DD.length() < 2) {
                DD = "0" + DD;
            }
            //计算年份
            String YY = "20" + HexUtils.get10Num(strs[17] + strs[18] + strs[0] + strs[1] + strs[2] + strs[3] + strs[8] + strs[9] + strs[10]).toString();

            String meterTime = YY + "-" + MM + "-" + DD + " " + HH + ":" + mm;

            meterData.setHeat(heat);
            meterData.setFlow(flow);
            meterData.setPower(power);
            meterData.setInstant(instant);
            meterData.setInlet(inlet);
            meterData.setOutlet(outlet);
            meterData.setWorkTime(workTime);
            meterData.setMeterTime(meterTime);
            SpringUtils.getBean(MeterDataServiceImpl.class).updateMeterData(meterData);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("表号：" + meterNo + " 数据解析错误，" + meterData.getReturnOrder());
        }
    }
}
